﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq.SqlClient;
namespace TraCuuTinRaoVat
{
    public partial class TraCuuNangCao : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                LinQ_RaoVatDataContext danhsach = new LinQ_RaoVatDataContext();
                var query = from Tin_Rao_Vat in danhsach.DIA_DIEMs
                            select Tin_Rao_Vat.TenDiaDiem;
                var query1 = from Tin_Rao_Vat in danhsach.DANH_MUC_CHINHs
                             select Tin_Rao_Vat;

                var query2 = from Tin_Rao_Vat in danhsach.DANH_MUC_CONs
                            // where Tin_Rao_Vat.MaDanhMucCon == 1
                             select Tin_Rao_Vat.TenDanhMucCon;

                dropDiaDiem.DataSource = query;
                dropDiaDiem.DataBind();

                string maDanhMucChinh = "MaDanhMucChinh";
                string tenDanhMucChinh = "TenDanhMucChinh";
                dropChuyenMucCha.DataSource = query1;
                dropChuyenMucCha.DataValueField = maDanhMucChinh;
                dropChuyenMucCha.DataTextField = tenDanhMucChinh;
                dropChuyenMucCha.DataBind();

           //     string maDanhMucCon = "MaDanhMucCon";
              //  string tenDanhMucCon = "TenDanhMucCon";
                dropChuyenMucCon.DataSource = query2;
               // dropChuyenMucCon.DataValueField = maDanhMucCon;
                //dropChuyenMucCon.DataTextField = tenDanhMucCon;
                dropChuyenMucCon.DataBind();

                dropDiaDiem.Items.Insert(0, "-- Tất cả địa điểm --");
                dropChuyenMucCha.Items.Insert(0, "-- Tất cả chuyên mục --");
                dropChuyenMucCon.Items.Insert(0, "-- Tất cả chuyên mục --");
            }
            else
            {
                Button1_Click1(sender, e);
            }
           
          
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
             
                TIN_RAO_VAT objTin = (TIN_RAO_VAT)e.Row.DataItem;
                HyperLink hplTenTin = (HyperLink)e.Row.FindControl("hplTenTin");
                hplTenTin.Text = objTin.TenTinRaoVat;
                hplTenTin.NavigateUrl = "~/ChiTietTinRaoVat.aspx?id=" + objTin.MaTinRaoVat;
                Label lblTenNguoiDung = (Label)e.Row.FindControl("lblTenNguoiDung");
                lblTenNguoiDung.Text = objTin.NGUOI_DUNG.TenNguoiDung;
            }
        }
     
        protected void Button1_Click1(object sender, EventArgs e)
        {
            try
            {
                int day = 100000;
                if (DropDownList1.SelectedIndex == 1)
                    day = 1;
                if (DropDownList1.SelectedIndex == 2)
                    day = 7;
                if (DropDownList1.SelectedIndex == 3)
                    day = 30;
                if (DropDownList1.SelectedIndex == 4)
                    day = 365;

                LinQ_RaoVatDataContext danhsach = new LinQ_RaoVatDataContext();
                if (TextBox1.Text == "Tất cả tin rao vặt")
                {
                    if ((dropDiaDiem.SelectedIndex == 0) && (dropChuyenMucCon.SelectedIndex == 0))
                    {
                        var query = from Tin_Rao_Vat in danhsach.TIN_RAO_VATs
                                    where Tin_Rao_Vat.ThoiGianDang.Value.AddDays(day) >= DateTime.Now
                                    select Tin_Rao_Vat;
                        DanhSachTinRaoVatTheoList1.ListTinRaoVat = query.ToList();

                        
                    }
                    if ((dropDiaDiem.SelectedIndex == 0) && (dropChuyenMucCon.SelectedIndex != 0))
                    {
                        var query = from Tin_Rao_Vat in danhsach.TIN_RAO_VATs
                                    where Tin_Rao_Vat.DANH_MUC_CON.TenDanhMucCon == dropChuyenMucCon.SelectedValue.ToString()
                                    && Tin_Rao_Vat.ThoiGianDang.Value.AddDays(day) >= DateTime.Now
                                    select Tin_Rao_Vat;
                        DanhSachTinRaoVatTheoList1.ListTinRaoVat = query.ToList();
                        
                    }
                    if ((dropDiaDiem.SelectedIndex != 0) && (dropChuyenMucCon.SelectedIndex == 0))
                    {
                        var query = from Tin_Rao_Vat in danhsach.TIN_RAO_VATs
                                    where Tin_Rao_Vat.DIA_DIEM.TenDiaDiem == dropDiaDiem.SelectedValue.ToString()
                                    && Tin_Rao_Vat.ThoiGianDang.Value.AddDays(day) > DateTime.Now
                                    select Tin_Rao_Vat;
                        DanhSachTinRaoVatTheoList1.ListTinRaoVat = query.ToList();
                        
                    }

                    if ((dropDiaDiem.SelectedIndex != 0) && (dropChuyenMucCon.SelectedIndex != 0))
                    {
                        var query = from Tin_Rao_Vat in danhsach.TIN_RAO_VATs
                                    where Tin_Rao_Vat.DIA_DIEM.TenDiaDiem == dropDiaDiem.SelectedValue.ToString()
                                    && Tin_Rao_Vat.DANH_MUC_CON.TenDanhMucCon == dropChuyenMucCon.SelectedValue.ToString()
                                    && Tin_Rao_Vat.ThoiGianDang.Value.AddDays(day) > DateTime.Now
                                    select Tin_Rao_Vat;
                        DanhSachTinRaoVatTheoList1.ListTinRaoVat = query.ToList();
                        
                    }

                }
                else
                {
                    if (TextBox1.Text.Trim() != "")
                    {
                        if ((dropDiaDiem.SelectedIndex == 0) && (dropChuyenMucCon.SelectedIndex == 0))
                        {
                            var query = from Tin_Rao_Vat in danhsach.TIN_RAO_VATs
                                        where (SqlMethods.Like(Tin_Rao_Vat.TenTinRaoVat, "%" + TextBox1.Text + "%") || SqlMethods.Like(Tin_Rao_Vat.NoiDungTinRaoVat, "%" + TextBox1.Text + "%"))
                                         && Tin_Rao_Vat.ThoiGianDang.Value.AddDays(day) >= DateTime.Now
                                        select Tin_Rao_Vat;
                            DanhSachTinRaoVatTheoList1.ListTinRaoVat = query.ToList();
                            
                        }
                        if ((dropDiaDiem.SelectedIndex == 0) && (dropChuyenMucCon.SelectedIndex != 0))
                        {
                            var query = from Tin_Rao_Vat in danhsach.TIN_RAO_VATs
                                        where (SqlMethods.Like(Tin_Rao_Vat.TenTinRaoVat, "%" + TextBox1.Text + "%") || SqlMethods.Like(Tin_Rao_Vat.NoiDungTinRaoVat, "%" + TextBox1.Text + "%"))
                                        && Tin_Rao_Vat.DANH_MUC_CON.TenDanhMucCon == dropChuyenMucCon.SelectedValue.ToString()
                                        && Tin_Rao_Vat.ThoiGianDang.Value.AddDays(day) >= DateTime.Now
                                        select Tin_Rao_Vat;
                            DanhSachTinRaoVatTheoList1.ListTinRaoVat = query.ToList();
                            
                        }
                        if ((dropDiaDiem.SelectedIndex != 0) && (dropChuyenMucCon.SelectedIndex == 0))
                        {
                            var query = from Tin_Rao_Vat in danhsach.TIN_RAO_VATs
                                        where (SqlMethods.Like(Tin_Rao_Vat.TenTinRaoVat, "%" + TextBox1.Text + "%") || SqlMethods.Like(Tin_Rao_Vat.NoiDungTinRaoVat, "%" + TextBox1.Text + "%"))
                                        && Tin_Rao_Vat.DIA_DIEM.TenDiaDiem == dropDiaDiem.SelectedValue.ToString()
                                        && Tin_Rao_Vat.ThoiGianDang.Value.AddDays(day) > DateTime.Now
                                        select Tin_Rao_Vat;
                            DanhSachTinRaoVatTheoList1.ListTinRaoVat = query.ToList();
                            
                        }

                        if ((dropDiaDiem.SelectedIndex != 0) && (dropChuyenMucCon.SelectedIndex != 0))
                        {
                            var query = from Tin_Rao_Vat in danhsach.TIN_RAO_VATs
                                        where (SqlMethods.Like(Tin_Rao_Vat.TenTinRaoVat, "%" + TextBox1.Text + "%") || SqlMethods.Like(Tin_Rao_Vat.NoiDungTinRaoVat, "%" + TextBox1.Text + "%"))
                                        && Tin_Rao_Vat.DIA_DIEM.TenDiaDiem == dropDiaDiem.SelectedValue.ToString()
                                        && Tin_Rao_Vat.DANH_MUC_CON.TenDanhMucCon == dropChuyenMucCon.SelectedValue.ToString()
                                        && Tin_Rao_Vat.ThoiGianDang.Value.AddDays(day) > DateTime.Now
                                        select Tin_Rao_Vat;
                            DanhSachTinRaoVatTheoList1.ListTinRaoVat = query.ToList();
                            
                        }
                    }
                }
            }
            catch (Exception ex)
            {
 
            }
           
        }
       
        protected void dropChuyenMucCha_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                LinQ_RaoVatDataContext danhsach = new LinQ_RaoVatDataContext();
                string maDanhMucCha = dropChuyenMucCha.SelectedValue.ToString();
                var query = from TIN_RAO_VAT in danhsach.DANH_MUC_CONs
                            where TIN_RAO_VAT.MaDanhMucChinh == int.Parse(maDanhMucCha)
                            select TIN_RAO_VAT.TenDanhMucCon;
                dropChuyenMucCon.DataSource = query;
                dropChuyenMucCon.DataBind();
                dropChuyenMucCon.Items.Insert(0, "-- Tất cả chuyên mục --");
            }
            catch (Exception ex)
            {
                LinQ_RaoVatDataContext danhsach = new LinQ_RaoVatDataContext();
                var query2 = from Tin_Rao_Vat in danhsach.DANH_MUC_CONs
                             select Tin_Rao_Vat.TenDanhMucCon;
                dropChuyenMucCon.DataSource = query2;              
                dropChuyenMucCon.DataBind();
                dropChuyenMucCon.Items.Insert(0, "-- Tất cả chuyên mục --");
            }
        }

     
    }
}